/**
 *
 */
package com.zhuoyue.elmt.web;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.utils.StringUtils;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.modules.sys.entity.Dict;
import com.eit.eitsite.modules.sys.entity.User;
import com.eit.eitsite.modules.sys.service.DictService;
import com.eit.eitsite.modules.sys.utils.UserUtils;
import com.zhuoyue.elmt.dao.DeviceParametersDao;
import com.zhuoyue.elmt.data.ResultData;
import com.zhuoyue.elmt.entity.DeviceParameters;
import com.zhuoyue.elmt.service.DeviceParametersService;
import com.zhuoyue.elmt.service.FailureReportService;
import com.zhuoyue.elmt.service.StatisticsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * 电梯查询Controller
 *
 * @author chenhao
 * @version 2016-06-12
 */
@Controller
@RequestMapping(value = "${adminPath}/elmt/statistics")
public class StatisticsController extends BaseController {

    @Autowired
    private StatisticsService statisticsService;

    @Autowired
    private FailureReportService failureReportService;

    @Autowired
    private DeviceParametersService deviceParametersService;

    @Autowired
    private DictService dictService;

    /**************************
     * 首页逾期、当天、本周维保设备的数据接口
     **********************************************/
    //首页逾期、当天、本周维保设备的数据
    @RequestMapping(value = {"listHomeStat"})
    public String listHomeStat(HashMap statParameters, HttpServletRequest request, HttpServletResponse response, Model model, String mainType) {
        User user = UserUtils.getUser();
        String wbdwdm = user.getCompany().getCode();
        String staff = user.getLoginName();

        ResultData<List<HashMap>> resultData = new ResultData();
        statParameters.put("dbName", Global.getConfig("jdbc.type"));
        statParameters.put("wbdwdm", wbdwdm);
        statParameters.put("staff", staff);
        statParameters.put("loginName", staff);
        // mainType 维保类型 halfmonth-半月 quarter-季度 halfyear-半年 year-年度
        statParameters.put("mainType", mainType);

        // 固定返回3条数据
        List<HashMap> retList = new ArrayList();
        for (int i = 0; i < 3; i++) {
            HashMap retMap = new HashMap();
            retMap.put("statType", "" + (i + 1));
            retMap.put("created_by", staff);
            retMap.put("mainten_unit_code", wbdwdm);
            retMap.put("sbcount", 0);
            retList.add(retMap);
        }

        List<HashMap> list = null;
        try {
            logger.info("首页逾期、当天、本周维保设备的数据");
            list = statisticsService.statDeviceCountByType(statParameters);
            if (list != null) {
                for (HashMap retMap : list) {
                    String statType = (String) retMap.get("statType");
                    for (HashMap newRetMap : retList) {
                        String newStatType = (String) newRetMap.get("statType");
                        if (statType.equals(newStatType)) {
                            newRetMap.put("sbcount", retMap.get("sbcount"));
                            break;
                        }
                    }
                }
                resultData.setCode("SUCC");
                resultData.setMessage("SUCCESS");
            } else {
                resultData.setCode("FAIL");
                resultData.setMessage("没有查询到符合条件数据！");
            }
            resultData.setList(retList);
        } catch (Exception e) {
            resultData.setCode("FAIL");
            resultData.setMessage("查询出现异常！");
            e.printStackTrace();
        }

        return super.renderString(response, resultData);
    }

    @RequestMapping(value = {"listHome"})
    public String listHome(HashMap statParameters, HttpServletRequest request, HttpServletResponse response, Model model, String mainType, String statType) {
        User user = UserUtils.getUser();
        String wbdwdm = user.getCompany().getCode();
        String staff = user.getLoginName();

        ResultData<List<HashMap>> resultData = new ResultData();
        statParameters.put("dbName", Global.getConfig("jdbc.type"));
        statParameters.put("wbdwdm", wbdwdm);
        statParameters.put("staff", staff);
        statParameters.put("loginName", staff);
        // mtType 维保类型 halfmonth-半月 quarter-季度 halfyear-半年 year-年度
        statParameters.put("mainType", mainType);
        statParameters.put("statType", statType);

        List<HashMap> list = null;
        try {
            logger.info("首页逾期、当天、本周维保设备的数据列表");
            list = statisticsService.statDeviceByType(statParameters);
            if (list != null) {
                resultData.setCode("SUCC");
                resultData.setMessage("SUCCESS");
            } else {
                resultData.setCode("FAIL");
                resultData.setMessage("没有查询到符合条件数据！");
            }
            resultData.setList(list);
        } catch (Exception e) {
            resultData.setCode("FAIL");
            resultData.setMessage("查询出现异常！");
            e.printStackTrace();
        }

        return super.renderString(response, resultData);
    }

    // 比例权重可以配置
    // (累计故障次数/设备数) * 50 + ((逾期数 + 维保记录不合格）/维保人员的维保记录的记录数） * 50
    // 故障来源维保记录要剔除
    // 排行榜
    @RequestMapping(value = {"statrank"})
    public String statrank(HashMap statParameters, HttpServletRequest request, HttpServletResponse response, Model model) {
        // 维保单位所在单位
        String wbdwdm = UserUtils.getUser().getCompany().getCode();

        ResultData<List<HashMap>> resultData = new ResultData();

        Dict qryDict = new Dict();
        qryDict.setType("STAT_WEIGHT");
        List<Dict> dictList = dictService.findList(qryDict);
        String failureWeight = "50";
        String mainWeight = "50";
        for (Dict dict : dictList) {
            if ("1".equals(dict.getValue())) {
                failureWeight = dict.getLabel();
            }
            if ("2".equals(dict.getValue())) {
                mainWeight = dict.getLabel();
            }
        }
        statParameters.put("failureWeight", failureWeight); // 故障权重
        statParameters.put("mainWeight", mainWeight); // 维保权重

        statParameters.put("dbName", Global.getConfig("jdbc.type"));
        statParameters.put("wbdwdm", wbdwdm);

        List<HashMap> list = null;
        try {
            logger.info("排行榜的数据");
            list = statisticsService.statrank(statParameters);
            if (list != null) {
                resultData.setCode("SUCC");
                resultData.setMessage("SUCCESS");
            } else {
                resultData.setCode("FAIL");
                resultData.setMessage("没有查询到符合条件数据！");
            }
            resultData.setList(list);
        } catch (Exception e) {
            resultData.setCode("FAIL");
            resultData.setMessage("查询出现异常！");
            e.printStackTrace();
        }

        return super.renderString(response, resultData);
    }

    @RequestMapping(value = {"statYearRemind"})
    public String statYearRemind(HashMap statParameters, HttpServletRequest request, HttpServletResponse response, Model model, String statType) {
        User user = UserUtils.getUser();
        String wbdwdm = user.getCompany().getCode();
        String staff = user.getLoginName();

        ResultData<List<DeviceParameters>> resultData = new ResultData();
        statParameters.put("dbName", Global.getConfig("jdbc.type"));
        statParameters.put("wbdwdm", wbdwdm);
        statParameters.put("loginName", staff);

        if (StringUtils.isEmpty(statType)) {
            statType = "inspect";
        }

        statParameters.put("statType", statType);

        List<DeviceParameters> list = null;
        try {
            logger.info("年检数据列表");
            list = statisticsService.statYearRemind(statParameters);
            if (list != null) {
                resultData.setCode("SUCC");
                resultData.setMessage("SUCCESS");
            } else {
                resultData.setCode("FAIL");
                resultData.setMessage("没有查询到符合条件数据！");
            }
            resultData.setList(list);
        } catch (Exception e) {
            resultData.setCode("FAIL");
            resultData.setMessage("查询出现异常！");
            e.printStackTrace();
        }

        return super.renderString(response, resultData);
    }

    // 按日期显示每天维保电梯数，显示15天(首页统计) 首页统计
    @RequestMapping(value = {"statEveryDayMainDevice"})
    public String statEveryDayMainDevice(HashMap statParameters, HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
        ResultData<List<HashMap>> resultData = new ResultData();
        List<HashMap> list = null;
        try {
            logger.info("按日期显示15天维保电梯数");
            statParameters.put("wbdwdm", UserUtils.getUser().getCompany().getCode());
            statParameters.put("loginName", UserUtils.getUser().getLoginName());
            statParameters.put("dbName", Global.getConfig("jdbc.type"));
            list = deviceParametersService.statEveryDayMainDevice(statParameters);
            if (list != null) {
                resultData.setCode("SUCC");
                resultData.setMessage("SUCCESS");
            } else {
                resultData.setCode("FAIL");
                resultData.setMessage("没有查询到符合条件数据！");
            }
            resultData.setList(list);
        } catch (Exception e) {
            resultData.setCode("FAIL");
            resultData.setMessage("查询出现异常！");
            e.printStackTrace();
        }
        return super.renderString(response, resultData);
    }

    // 按日期显示各个维保小组的维保数量 首页统计
    @RequestMapping(value = {"statEveryDayMainDeviceList"})
    public String statEveryDayMainDeviceList(String groupid, String oneday, HashMap statParameters, HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
        ResultData<List<DeviceParameters>> resultData = new ResultData();
        List<DeviceParameters> list = null;
        try {
            logger.info("按日期显示15天维保电梯列表");
            statParameters.put("wbdwdm", UserUtils.getUser().getCompany().getCode());
            statParameters.put("loginName", UserUtils.getUser().getLoginName());
            statParameters.put("dbName", Global.getConfig("jdbc.type"));
            statParameters.put("oneday", oneday);
            if ("0".equals(groupid)) {
                groupid = "zero";
            }
            statParameters.put("groupid", "1");
            list = deviceParametersService.statEveryDayMainDeviceGroupList(statParameters);
            if (list != null) {
                resultData.setCode("SUCC");
                resultData.setMessage("SUCCESS");
            } else {
                resultData.setCode("FAIL");
                resultData.setMessage("没有查询到符合条件数据！");
            }
            resultData.setList(list);
        } catch (Exception e) {
            resultData.setCode("FAIL");
            resultData.setMessage("查询出现异常！");
            e.printStackTrace();
        }
        return super.renderString(response, resultData);
    }
}